// pages/jietu/jietu.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    appurl: app.apiurl,
apiimg: app.apiimg,
    jietu1:true,
    jietu2:false,
    ggid:'',
gglist:'',
jietu:app.apiimg + 'image/icon11.png'
  },
  jietu(){
    let _this = this
    if(_this.data.jietu == _this.data.apiimg + '/image/icon11.png'){
      _this.setData({
        jietu1:true
      })
    } else {
      _this.setData({
        jietu1:false
      })
    }
  },
  open(){
    let _this = this
    _this.setData({
      jietu2:true
    })
  },
  upload() {
    let that = this
    wx.chooseMedia({
      count: 1,
      mediaType: ['image'],
      sourceType: ['album', 'camera'],
      maxDuration: 30,
      camera: 'back',
      success(res) {
        var tempFiles = res.tempFiles[0]['tempFilePath'];
        var fileSize = res.tempFiles[0]['size'];
        var MAX_SIZE = Number(that.data.peizhi.config_param39) * 1024 * 1024;
        if (fileSize > MAX_SIZE) {
          wx.showToast({
            title: '文件超过大小限制',
            icon: 'none',
          });
          // 文件超过限制，处理逻辑，比如重新选择
          return;
        } else {
          wx.uploadFile({
            url: app.apiurl + 'api/common/upload',
            filePath: tempFiles,
            name: 'file',
            formData: {},
            success(res) {
              const data = JSON.parse(res.data)
              that.setData({
                jietu:data.data.fullurl
              })
              that.z_jietu1()
            }
          })
        }
      }
    })
  },
  close(){
    let _this = this
    _this.setData({
      jietu2:false
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(wx.getStorageSync("user_token") == ""){
      wx.navigateTo({
        url: '/pages/login/login'
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.z_jietu1()
    this.guanggao1()
    this.z_peizhi()
  },
  z_peizhi() {
    let _this = this
    let params = {
      "token":wx.getStorageSync("user_token"),
    }
    wx.request({
      url: app.apiurl + 'api/config/configBase',
      method: 'GET',
      data: params,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 修改请求
      },
      success(res) {
        _this.setData({
          peizhi:res.data.data
        })
      }
    })
  },
  z_jietu1(){
    let _this = this
    if(_this.data.jietu == _this.data.apiimg + '/image/icon11.png'){
      _this.setData({
        jietu1:true
      })
    } else {
      _this.setData({
        jietu1:false
      })
    }
  },
  guanggao1(){
    let _this = this
    let params = {
      "token":wx.getStorageSync("user_token"),
    }
    wx.request({
      url: app.apiurl + 'api/config/advertPage',
      method: 'POST',
      data: params,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 修改请求
      },
      success(res) {
         _this.setData({
           ggid:res.data.data[0].id
         })
         _this.guanggao2()
      }
    })
  },
  guanggao2(){
    let _this = this
    let params = {
      "token":wx.getStorageSync("user_token"),
    }
    wx.request({
      url: app.apiurl + 'api/config/advert?page_id=' + _this.data.ggid,
      method: 'GET',
      data: params,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 修改请求
      },
      success(res) {
         _this.setData({
           gglist:res.data.data[0]
         })
      }
    })
  },
  z_kvlianjia(e){
    let _this = this
    let link_url = e.currentTarget.dataset.url
    let id = e.currentTarget.dataset.url
    if(link_url != undefined){
      wx.navigateTo({
        url: link_url,
      })
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})